package com.aso114.project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.bean.PriceListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OpenVipAdapter extends BaseAdapter {
    private OnItemClickListener mListener;
    private List<PriceListBean> priceListBean = new ArrayList<>();
    private Context context;


    public OpenVipAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return priceListBean.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_open_vip, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (priceListBean.get(position).isClick()){
            holder.openVipCheck.setImageResource(R.mipmap.buy_check_ic);
        }else {
            holder.openVipCheck.setImageResource(R.mipmap.buy_uncheck_iccopy);
        }
        holder.openVipTitle.setText((int) priceListBean.get(position).getVipNum() / 30 + "个月");
        holder.openVipPrice1.setText("￥" + String.format("%.2f", priceListBean.get(position).getVipPrice()));
        holder.openVipPrice2.setText("￥" + String.format("%.2f", priceListBean.get(position).getVipPrice() / (priceListBean.get(position).getVipNum() / 30)) + "/月");
        holder.openVipLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(position);
                }
            }
        });
        return convertView;
    }

    public void setData(List<PriceListBean> priceListBean) {
        this.priceListBean = priceListBean;
    }

    static class ViewHolder {
        @BindView(R.id.open_vip_check)
        ImageView openVipCheck;
        @BindView(R.id.open_vip_title)
        TextView openVipTitle;
        @BindView(R.id.open_vip_price1)
        TextView openVipPrice1;
        @BindView(R.id.open_vip_price2)
        TextView openVipPrice2;
        @BindView(R.id.open_vip_ly)
        LinearLayout openVipLy;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
